package com.atguigu.flink.chapter07.state;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.state.AggregatingState;
import org.apache.flink.api.common.state.AggregatingStateDescriptor;
import org.apache.flink.api.common.typeinfo.TypeHint;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.contrib.streaming.state.EmbeddedRocksDBStateBackend;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/12/15 9:26
 */
public class Flink10_State_Backend {
    public static void main(String[] args) throws Exception {
        System.setProperty("HADOOP_USER_NAME", "atguigu");
        
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        env.enableCheckpointing(3000); // 启用checkpoint, 才会把状态保存到远程(持久化)
        
        // 1. 内存
        // 旧
//        env.setStateBackend(new MemoryStateBackend());  // 默认配置
        // 新的
//        env.setStateBackend(new HashMapStateBackend());
//        env.getCheckpointConfig().setCheckpointStorage(new JobManagerCheckpointStorage());
        
        // 2. fs
        // 旧
//        env.setStateBackend(new FsStateBackend("hdfs://hadoop162:8020/ck"));
        // 新
//        env.setStateBackend(new HashMapStateBackend());
//        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/ck1");
        
        // 3. rocksdb
//        env.setStateBackend(new RocksDBStateBackend("hdfs://hadoop162:8020/ck2"));
        // 新
        env.setStateBackend(new EmbeddedRocksDBStateBackend());
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/ck3");
        
        env
            .socketTextStream("hadoop162", 9999)
            .map(line -> {
                String[] data = line.split(",");
                return new WaterSensor(data[0], Long.valueOf(data[1]), Integer.valueOf(data[2]));
            })
            .keyBy(WaterSensor::getId)
            .process(new KeyedProcessFunction<String, WaterSensor, String>() {
                
                private AggregatingState<WaterSensor, Double> vcAvgState;
                
                @Override
                public void open(Configuration parameters) throws Exception {
                    vcAvgState = getRuntimeContext().getAggregatingState(new AggregatingStateDescriptor<WaterSensor, Tuple2<Integer, Long>, Double>(
                        "vcAvgState",
                        new AggregateFunction<WaterSensor, Tuple2<Integer, Long>, Double>() {
                            @Override
                            public Tuple2<Integer, Long> createAccumulator() {
                                return Tuple2.of(0, 0L);
                            }
                            
                            @Override
                            public Tuple2<Integer, Long> add(WaterSensor value, Tuple2<Integer, Long> acc) {
                                return Tuple2.of(acc.f0 + value.getVc(), acc.f1 + 1);
                            }
                            
                            @Override
                            public Double getResult(Tuple2<Integer, Long> acc) {
                                return acc.f0 * 1.0 / acc.f1;
                            }
                            
                            @Override
                            public Tuple2<Integer, Long> merge(Tuple2<Integer, Long> a, Tuple2<Integer, Long> b) {
                                return null;
                            }
                        },
//                        Types.TUPLE(Types.INT, Types.LONG)
                        TypeInformation.of(new TypeHint<Tuple2<Integer, Long>>() {})
                    ));
                }
                
                @Override
                public void processElement(WaterSensor value,
                                           Context ctx,
                                           Collector<String> out) throws Exception {
                    vcAvgState.add(value);
                    
                    out.collect(ctx.getCurrentKey() + " " + vcAvgState.get());
                }
            })
            .print();
        
        env.execute();
    }
    
}
